/*
 * $Id: ResourceBundleMessageController.java 46 2013-05-03 21:38:12Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 46 $
 * $Date: 2013-05-03 21:38:12 +0000 (Fri, 03 May 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.web.controller.admin;

import com.sci.logisticsmap.service.ResourceBundleMessageService;
import com.sci.logisticsmap.support.Page;
import com.sci.logisticsmap.service.model.ResourceBundleMessageDTCommand;
import com.sci.logisticsmap.service.model.ResourceBundleMessageLightDTModel;
import com.sci.logisticsmap.web.support.WebUtilities;
import com.sci.logisticsmap.web.model.ResourceBundleMessageVWCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.PostConstruct;

/**
 * The Controller for processing resource bundle message admin view/ajax requests.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 46 $
 */
@Controller("admin.ResourceBundleMessage")
@RequestMapping("")
public class ResourceBundleMessageController extends BaseController {
    /**
     * The resource bundle message admin path.
     */
    public static final String PATH_ADMIN_RESOURCE_BUNDLE_MESSAGE = "/admin/resource-bundle-message";
    /**
     * The resource bundle message edit admin path.
     */
    public static final String PATH_ADMIN_RESOURCE_BUNDLE_MESSAGE_EDIT = "/admin/resource-bundle-message/edit";
    /**
     * The resource bundle message list admin path.
     */
    public static final String PATH_ADMIN_RESOURCE_BUNDLE_MESSAGE_LIST = "/admin/resource-bundle-message/list";

    /**
     * The resource bundle message edit admin view.
     */
    private final String viewEdit = "admin/resourceBundleMessage/editPage";
    /**
     * The resource bundle message list admin view.
     */
    private final String viewList = "admin/resourceBundleMessage/listPage";

    /**
     * The resource bundle message service.
     */
    @Autowired
    private ResourceBundleMessageService resourceBundleMessageService;

    /**
     * The default constructor.
     * Initializes a Controller class.
     */
    public ResourceBundleMessageController() {
    }

    /*
     * (non-Javadoc)
     *
     * @see BaseController#init()
     */
    @PostConstruct
    public void init() {
        super.init();
    }

    /**
     * The method to response on the resource bundle message edit admin view request.
     *
     * @param  code
     *         The code.
     * @param  model
     *         The model.
     *
     * @return the resource bundle message edit admin view.
     */
    @RequestMapping(value=PATH_ADMIN_RESOURCE_BUNDLE_MESSAGE_EDIT, method=RequestMethod.GET)
    public String onEdit(@RequestParam(value="code") String code, Model model) {
        try {
            ResourceBundleMessageDTCommand messageCommand = resourceBundleMessageService.getDTCommand(code);
            model.addAttribute("messageCommand", conversionUtilities.convert(messageCommand, ResourceBundleMessageVWCommand.class));
        } catch (Exception e) {
            addExceptionToModel(model, e);
        }

        return viewEdit;
    }

    /**
     * The method to response on the resource bundle message edit admin view submitting request.
     *
     * @param  command
     *         The command.
     * @param  result
     *         The validation binding result.
     //* @param  code
     //*         The code.
     * @param  model
     *         The model.
     *
     * @return the resource bundle message edit admin view.
     */
    @RequestMapping(value=PATH_ADMIN_RESOURCE_BUNDLE_MESSAGE_EDIT, method=RequestMethod.POST)
    public String onEditSubmit(@ModelAttribute("messageCommand") ResourceBundleMessageVWCommand command, BindingResult result/*, @RequestParam(value="code") String code*/, Model model) {
        try {
            boolean failed = false;
            if (!result.hasErrors()) {
                try {
                    resourceBundleMessageService.edit(conversionUtilities.convert(command, ResourceBundleMessageDTCommand.class));
                    return buildRedirectToPath(PATH_ADMIN_RESOURCE_BUNDLE_MESSAGE_LIST);
                } catch (Exception e) {
                    addExceptionToModel(model, e);
                    failed = true;
                }
            } else {
                failed = true;
            }

            if (failed) {
            }
        } catch (Exception e) {
            addExceptionToModel(model, e);
        }

        return viewEdit;
    }

    /**
     * The method to response on the resource bundle message list admin view request.
     *
     * @param  page
     *         The page number.
     * @param  keyword
     *         The keyword.
     * @param  model
     *         The model.
     *
     * @return the resource bundle message list admin view.
     */
    @RequestMapping(value={PATH_ADMIN_RESOURCE_BUNDLE_MESSAGE, PATH_ADMIN_RESOURCE_BUNDLE_MESSAGE_LIST}, method={RequestMethod.GET})
    public String onList(@RequestParam(value="page", required=false) Integer page,
                         @RequestParam(value="keyword", required=false) String keyword, Model model) {
        try {
            Page<ResourceBundleMessageLightDTModel> messagePage = resourceBundleMessageService.getLightDTModels(WebUtilities.validatePageRequestParam(page));
            model.addAttribute("messagePage", messagePage);
        } catch (Exception e) {
            addExceptionToModel(model, e);
        }

        return viewList;
    }
}
